import axios from "axios";
import { notification } from "ant-design-vue";
import "ant-design-vue/lib/notification/style/css";


const request = axios.create({
  baseURL: process.env.VUE_APP_BASE_API,
  timeout: 10000,
});

request.interceptors.request.use(
  (config) => {
    //请求头设置token
    // const token = store.getters.getToken;
    // if (token) {
    //   config.headers["Access-Token"] = "Bearer " + token;
    // }
    console.log("请求参数:", config);
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

request.interceptors.response.use(
  (response) => {
    console.log("返回结果", response);
    //返回数据
    const resp = response.data;
    const message = resp.message || "返回数据异常";
    //401状态处理
    // if (resp.code === 401) {
    //   notification.error({
    //     message: "消息提示",
    //     description: message,
    //     duration: 2,
    //     onClose: () => {
    //       //清除token状态
    //       // store.commit("login/setToken", []);
    //       // removeToken("token");
    //       //跳转到登录页
    //       location.href = "/login";
    //     },
    //   });

    //   return Promise.reject(new Error("token失效"));
    // }
    //非200状态码提示
    if (resp.code != 200) {
      notification.error({
        message: "消息提示",
        description: message,
        duration: 3,
      });
      return Promise.reject(new Error(message || "Error"));
    }
    return resp;
  },
  (error) => {
    return Promise.reject(error);
  }
);

export default request;

